class BuyRecordModel {
  final int id;
  final String symbol;
  final String name;
  final double buyPrice;
  final int quantity;
  final int remainingQuantity;
  final DateTime buyDate;
  final DateTime createdAt;

  BuyRecordModel({
    required this.id,
    required this.symbol,
    required this.name,
    required this.buyPrice,
    required this.quantity,
    required this.remainingQuantity,
    required this.buyDate,
    DateTime? createdAt,
  }) : createdAt = createdAt ?? DateTime.now();

  // 计算买入总金额
  double get totalAmount {
    return buyPrice * quantity;
  }

  // 是否已完全卖出
  bool get isCleared {
    return remainingQuantity == 0;
  }

  // 从Map创建对象
  factory BuyRecordModel.fromMap(Map<String, dynamic> map) {
    return BuyRecordModel(
      id: map['id'],
      symbol: map['symbol'],
      name: map['name'],
      buyPrice: map['buyPrice'],
      quantity: map['quantity'],
      remainingQuantity: map['remainingQuantity'],
      buyDate: DateTime.parse(map['buyDate']),
      createdAt: DateTime.parse(map['createdAt']),
    );
  }

  // 转换为Map
  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'symbol': symbol,
      'name': name,
      'buyPrice': buyPrice,
      'quantity': quantity,
      'remainingQuantity': remainingQuantity,
      'buyDate': buyDate.toIso8601String(),
      'createdAt': createdAt.toIso8601String(),
    };
  }
}